package com.zhang.zhangapp.view.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.bean.location.CityDao;
import com.zhang.zhangapp.utils.ScreenUtils;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.custom.FilterPopupWindow;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/15.
 */
@ContentView(R.layout.activity_shop_list)
public class ShopListActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    @ViewInject(R.id.nearby_nearby)
    TextView nearbyText;
    @ViewInject(R.id.nearby_order)
    TextView orderText;
    @ViewInject(R.id.nearby_filter)
    TextView filterText;
    @ViewInject(R.id.filters_layout)
    LinearLayout filterLayout;
    Map<String, String> params = new HashMap<>();
    private String typeCode;
    private String keyword;

    List<Map<String, String>> nearbyMapList = new ArrayList<>();//附近数据
    List<Map<String, String>> orderMapList = new ArrayList<>();//排序数据
    List<Map<String, String>> filterMapList = new ArrayList<>();//筛选数据

    FilterPopupWindow nearbyPopup;
    FilterPopupWindow orderPopup;
    FilterPopupWindow filterPopup;
    int[] popWindowLoc;//弹出菜单位置
    ShopFragment fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        typeCode = getIntent().getStringExtra("typeCode");
        keyword = getIntent().getStringExtra("keyword");
        x.view().inject(this);
        titleText.setText(getIntent().getStringExtra("title"));
        fragment = new ShopFragment();
        Bundle bundle = new Bundle();
        bundle.putString("methodName", "hzg/shop/queryShop");
        params.put("lat", MApplication.getInstance().getMapLocation().getLatitude() + "");
        params.put("lon", MApplication.getInstance().getMapLocation().getLongitude() + "");
        params.put("range", "");//距离多少米（1000,3000,5000,10000,附近传1000）
        params.put("consumeRegion", "");//人均消费区间（0-50,50-100,100-300,300-500）
        params.put("consumeRegion", "");//人均消费区间（0-50,50-100,100-300,300-500）
        params.put("orderkey", "");//排序规则(1 人气优先，2 离我最近，3 人均最高，4 人均最低，5 最新发布)
        params.put("cityCode", CityDao.queryCityCode(MApplication.getInstance().getMapLocation().getCity().replace("市", "")));
        params.put("keywords", keyword == null ? "" : keyword);
        params.put("pageSize", BaseConfig.PAGE_SIZE + "");
        params.put("typeCode", typeCode);
        bundle.putSerializable("params", (Serializable) params);
        fragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
        initFilters();
    }

    private void initFilters() {
        String[] nearbyArr = new String[]{"全部", "1", "3", "5", "10", "附近"};
        for (String str : nearbyArr) {
            Map<String, String> map = new HashMap<>();
            map.put("value", str);
            if ("全部".equals(str))
                map.put("value", "");
            if ("附近".equals(str))
                map.put("value", "1");

            map.put("label", str);
            map.put("selected", "false");
            nearbyMapList.add(map);
        }

        String[] filterArr = new String[]{"50以下", "50-100", "100-300", "300以上"};
        for (String str : filterArr) {
            Map<String, String> map = new HashMap<>();
            map.put("value", str);
            if ("50以下".equals(str))
                map.put("value", "0-50");
            if ("300以上".equals(str))
                map.put("value", "300-500");
            map.put("label", str);
            map.put("selected", "false");
            filterMapList.add(map);
        }

        String[] orderArr = new String[]{"人气优先", "离我最近", "人均最高", "人均最低", "最新发布"};
        for (int i = 0; i < orderArr.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("value", (i + 1) + "");
            map.put("label", orderArr[i]);
            map.put("selected", "false");
            orderMapList.add(map);
        }
    }

    @Event(type = View.OnClickListener.class, value = R.id.action_bar_left_image)
    private void back(View view) {
        this.finish();
    }

    @Event(type = View.OnClickListener.class, value = {R.id.nearby_filter, R.id.nearby_nearby, R.id.nearby_order})
    private void onFilterClick(View view) {
        switch (view.getId()) {
            case R.id.nearby_filter: {
                if (popWindowLoc == null)
                    popWindowLoc = ScreenUtils.calculatePopWindowPos(filterLayout);
                if (filterPopup == null) {
                    filterPopup = new FilterPopupWindow(this, filterMapList, popWindowLoc[1]);
                    filterPopup.setonItemClick(new FilterPopupWindow.onItemClick() {
                        @Override
                        public void onItemClick(Map<String, String> map) {
                            filterPopup.dismiss();
                            filterText.setSelected(false);
                            fragment.updateParams("consume", map.get("value"));
                        }
                    });
                    filterPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            filterText.setSelected(false);
                        }
                    });
                }
                filterPopup.showAtLocation(filterLayout, Gravity.NO_GRAVITY, popWindowLoc[0], popWindowLoc[1]);
                filterText.setSelected(true);
            }
            break;
            case R.id.nearby_nearby:
                if (popWindowLoc == null)
                    popWindowLoc = ScreenUtils.calculatePopWindowPos(filterLayout);
                if (nearbyPopup == null) {
                    nearbyPopup = new FilterPopupWindow(this, nearbyMapList, popWindowLoc[1]);
                    nearbyPopup.setonItemClick(new FilterPopupWindow.onItemClick() {
                        @Override
                        public void onItemClick(Map<String, String> map) {
                            nearbyPopup.dismiss();
                            nearbyText.setSelected(false);
                            fragment.updateParams("range", map.get("value"));
                        }
                    });
                    nearbyPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            nearbyText.setSelected(false);
                        }
                    });
                }
                nearbyPopup.showAtLocation(filterLayout, Gravity.NO_GRAVITY, popWindowLoc[0], popWindowLoc[1]);
                nearbyText.setSelected(true);
                break;
            case R.id.nearby_order:
                if (popWindowLoc == null)
                    popWindowLoc = ScreenUtils.calculatePopWindowPos(filterLayout);
                if (orderPopup == null) {
                    orderPopup = new FilterPopupWindow(this, orderMapList, popWindowLoc[1]);
                    orderPopup.setonItemClick(new FilterPopupWindow.onItemClick() {
                        @Override
                        public void onItemClick(Map<String, String> map) {
                            params.put("orderkey", map.get("value"));
                            orderPopup.dismiss();
                            orderText.setSelected(false);
                            fragment.updateParams("orderkey", map.get("value"));
                        }
                    });
                    orderPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            orderText.setSelected(false);
                        }
                    });
                }
                orderPopup.showAtLocation(filterLayout, Gravity.NO_GRAVITY, popWindowLoc[0], popWindowLoc[1]);
                orderText.setSelected(true);
                break;
        }

    }
}
